package App.Tencent;

import com.tencentyun.TLSSigAPIv2;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.logging.Logger;

/**
 * Created by xw on 2018/11/28.
 */
public class SignatureManager {

    private static Logger _log = Logger.getLogger("SignatureManager");

    public static String getSignature(long _sdkid, String winKeyPath, String linuxKeyPath, String _userid) {

        String strSig = "";
        try {

            String _keyPath = linuxKeyPath;
            String os = System.getProperty("os.name");
            if (os.toLowerCase().startsWith("win")) {
                _keyPath = winKeyPath;
            }

            File priKeyFile = null;
            if (null != _keyPath) {
                priKeyFile = new File(_keyPath);
                StringBuilder strBuilder = new StringBuilder();
                String s = "";

                BufferedReader br = new BufferedReader(new FileReader(priKeyFile));
                while ((s = br.readLine()) != null) {
                    strBuilder.append(s + '\n');
                }
                br.close();
                String priKey = strBuilder.toString();
                tls_sigature.GenTLSSignatureResult result = tls_sigature.GenTLSSignatureEx(_sdkid, _userid, priKey);
                if (result.urlSig.length() > 0) {
                    strSig = result.urlSig;
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return strSig;
    }

    public static String getSignatureAdv(long _sdkid, String winKeyPath, String linuxKeyPath, String _userid, int sigType, String securetKey) {
        if (sigType == 0) {
            return getSignature(_sdkid, winKeyPath, linuxKeyPath, _userid);
        } else {
            TLSSigAPIv2 api = new TLSSigAPIv2(_sdkid, securetKey);
            return api.genSig(_userid, 180 * 86400);//默认180天
        }
    }
}
